<template>
  <div class="login">
    <div class="name">分行者跟单社区</div>
    <div class="small">MT4/MT5跟单系统社区一站式服务</div>
    <el-form size="small" class="login_form" ref="form" label-width="0px">

      <el-form-item>
        <div class="type fla">
          <span class="ft-15 mgr-5" @click="onNumTab" :class="isActive === 1 ? 'active' : ''">手机号</span>
          <span class="ft-12 email" @click="onEmailTab" :class="isActive === 1 ? 'emailActive' : ''">邮箱</span>
        </div>
      </el-form-item>
      <!-- 手机号 -->
      <div class="fl" v-if="isActive === 0" style="display: flex;flex-direction: column;">

        <div style="display: flex;">
          <el-form-item>
            <el-select v-model="value" placeholder="+86" style="display: block">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="phone">
            <el-input placeholder="11位手机号" prefix-icon="el-icon-user" v-model="phone" style="width: 297px;" type="number">
            </el-input>
          </el-form-item>
        </div>

        <div style="">
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input placeholder="请输入密码" prefix-icon="el-icon-lock" v-model="password" show-password>
            </el-input>
          </el-form-item>
          <!-- 再次输入密码 -->
          <el-form-item prop="rePwd">
            <el-input placeholder="确认密码" prefix-icon="el-icon-edit" v-model="rePwd" show-password>
            </el-input>
          </el-form-item>
          <!-- 验证码 -->
          <div class="ft">
            <div class="fl">
              <el-form-item prop="sms_code">
                <el-input style="width: 280px;" prefix-icon="el-icon-message" v-model="yzcode" placeholder="验证码"
                  type="number" clearable></el-input>
              </el-form-item>
            </div>
            <el-form-item>
              <div class="sms_code" @click="getYZM">{{ codeTxt }}</div>
            </el-form-item>
          </div>
          <!-- 推广码 -->
          <el-form-item prop="code">
            <el-input v-model="code" placeholder="请输入推广码" type="number" style="height: 100%;"></el-input>
          </el-form-item>
          <!-- 注册 -->
          <div class="ft">
            <el-form-item>
              <el-button type="success" size="mini" @click="onRegister">注册</el-button>
            </el-form-item>
            <div class="fc account" @click="login">使用已有账户登录</div>
          </div>
        </div>


      </div>

      <!-- 邮箱 -->
      <div v-if="isActive === 1">
        <el-form-item prop="email">
          <el-input placeholder="请输入邮箱" prefix-icon="el-icon-user" v-model="email">
          </el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" prefix-icon="el-icon-lock" v-model="password" show-password>
          </el-input>
        </el-form-item>
        <!-- 再次输入密码 -->
        <el-form-item prop="rePwd">
          <el-input placeholder="确认密码" prefix-icon="el-icon-edit" v-model="rePwd" show-password>
          </el-input>
        </el-form-item>
        <!-- 验证码 -->
        <div class="ft">
          <div class="fl">
            <el-form-item prop="sms_code">
              <el-input style="width: 280px;" prefix-icon="el-icon-message" v-model="yzcode" placeholder="验证码"
                type="number" clearable></el-input>
            </el-form-item>
          </div>
          <el-form-item>
            <div class="sms_code" @click="getYZM1">{{ codeTxt }}</div>
          </el-form-item>
        </div>
        <!-- 推广码 -->
        <el-form-item prop="code">
          <el-input v-model="code" placeholder="请输入推广码" type="number" style="height: 100%;"></el-input>
        </el-form-item>
        <!-- 注册 -->
        <div class="ft">
          <el-form-item>
            <el-button type="success" size="mini" @click="onRegister1">注册</el-button>
          </el-form-item>
          <div class="fc account" @click="login">使用已有账户登录</div>
        </div>
      </div>

    </el-form>
  </div>
</template>
<script>
import { registerApi, codeApi, sendSmsApi } from '@/api/index'
export default {
  name: 'LoginView',
  data() {
    return {
      isActive: 0,
      phone: '',
      password: '',
      rePwd: '',
      yzcode: '',
      time: 60000,
      timer: null,
      email: '',
      code: '',
      type: 0,
      invCode: "",
      codeTxt: "获取验证码",
      status: null,

      options: [{
        value: '选项1',
        label: '+86'
      }, {
        value: '选项2',
        label: '+87'
      }],
      value: ''
    }
  },
  created() {
    /* console.log(opt)
      if (opt.isPhone == "true") {
        this.phone = opt.account
        this.reg_type = 0
        this.type = 0
      } else {
        this.email = opt.account
        this.reg_type = 2
        this.type = 1
        console.log(222)
      }
      this.invCode = opt.invCode */
  },

  methods: {
    changeLogin() {
      this.isLogin = !this.isLogin
    },
    login() {
      this.$router.push('login')
    },
    onNumTab() {
      this.isActive = 0
    },
    onEmailTab() {
      this.isActive = 1
    },

    onRegister() {

        if (!this.phone) {
          this.$message.error("手机号不能为空");
          return;
        }
        if (!this.password) {
          this.$message.error("密码不能为空");
          return;
        }

        if (!this.code) {
          this.$message.error("邀请码不能为空");
          return;
        }

        if (this.password !== this.rePwd) {
          this.$message.error("两次密码输入必须一致");
          return;
        }
      const params = {
        code: this.invCode
      }
      let data = {
        reg_type: this.reg_type,
        code: this.code,
        phone: this.phone,
        //email: this.email,
        password: this.password,
        sms_code: this.yzcode,

      };
      registerApi(data).then(res => {
        console.log(res.data);
        localStorage.setItem("token", res.data.data.token);
        this.$store.commit("CHANGE_LOGIN", true);

        this.$message.success(res.data.msg)
        setTimeout(() => {
          this.login()
        }, 500);

      });
    },

    onRegister1() {

        if (!this.email) {
          this.$message.error("邮箱不能为空");
          return;
        }
        if (!this.password) {
          this.$message.error("密码不能为空");
          return;
        }

        if (!this.code) {
          this.$message.error("邀请码不能为空");
          return;
        }

        if (this.password !== this.rePwd) {
          this.$message.error("两次密码输入必须一致");
          return;
        }
      const params = {
        code: this.code
      }
      let data = {
        reg_type:2,
        code: this.code,
        //phone: this.phone,
        email: this.email,
        password: this.password,
        sms_code: this.yzcode,
      };
      registerApi(data).then(res => {
        console.log(res.data);
        localStorage.setItem("token", res.data.data.token);
        this.$store.commit("CHANGE_LOGIN", true);

        this.$message.success(res.data.msg)
        setTimeout(() => {
          this.login()
        }, 500);

      });
    },

    updateImageCode() {
      this.showLoading("加载中");
      setTimeout(() => {
        this.hideLoading();
        let time = new Date().getTime();
      }, 500);
    },


    getYZM() {
      if (this.isActive === 0 && this.phone === "") {
        this.$message.error("请输入正确手机号");
        return;
      }
      /* if (this.isActive === 1 && this.email === "") {
        this.$message.error("请输入正确邮箱地址");
        return;
      } */
      if (this.time !== 60000) {
        return;
      }

      let data = {
        type: 0,
        phone: this.phone,
        email: this.email
      };

      sendSmsApi(data).then(res => {
        this.timer = setInterval(() => {
          this.time -= 30;
          this.codeTxt = Math.floor(this.time / 1000) + "s后重试";
          if (this.time < 30) {
            clearInterval(this.timer);
            this.time = 60000;
            this.codeTxt = "获取验证码";
          }
        }, 30);

        console.log(res.data)
      });
    },

    getYZM1() {
      /* if (this.isActive === 0 && this.phone === "") {
        this.$message.error("请输入正确手机号");
        return;
      } */
       if (this.isActive === 1 && this.email === "") {
        this.$message.error("请输入正确邮箱地址");
        return;
      } 
      if (this.time !== 60000) {
        return;
      }

      let data = {
        type: 2,
        //phone: this.phone,
        email: this.email
      };

      sendSmsApi(data).then(res => {
        this.timer = setInterval(() => {
          this.time -= 30;
          this.codeTxt = Math.floor(this.time / 1000) + "s后重试";
          if (this.time < 30) {
            clearInterval(this.timer);
            this.time = 60000;
            this.codeTxt = "获取验证码";
          }
        }, 30);

        console.log(res.data)
      });
    },

  }
}
</script>

<style scoped lang="scss">
::v-deep input::-webkit-outer-spin-button,
::v-deep input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}

.login {
  // margin-top: 200px;
  background: url("@/assets/img/login-background.svg") no-repeat center;
  // height: 942px;
  width: 100%;
  background-size: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;

  .name {
    margin-top: 40px;
    font-size: 26px;
    font-weight: 600;
  }

  .small {
    font-size: 12px;
    color: rgba(0, 0, 0, 0.45);
    margin-top: 12px;
    margin-bottom: 40px;

  }

  .login_form {
    min-width: 260px;
    width: 368px;
    font-size: 14px;
    font-variant: tabular-nums;
    line-height: 1.5;

    .type {
      width: 100%;
      text-align: start;
      cursor: pointer;
    }

    .el-button {
      width: 100%;
      background-color: #04d1b3;
      border-color: #04d1b3;
      text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.12);
      box-shadow: 0 2px 0 rgba(0, 0, 0, 0.045);
      font-size: 16px;
    }
  }

  .account {
    font-size: 14px;
    color: #04d1b3;
    cursor: pointer;
  }

  ::v-deep .el-button[data-v-c38accf0] {
    font-size: 12px !important;
    width: 100px !important;
    margin-top: 20px !important;
  }

  .sms_code {
    font-size: 12px;
    height: 32px;
    border: 1px solid #DCDFE6;
    border-radius: 4px;
    padding: 0 5px;
    margin-left: 10px;
  }
}

.el-form .el-select {
  width: 70px;
}

.email {
  color: rgb(153, 153, 153);
}

.active {
  font-size: 12px;
  color: rgb(153, 153, 153);
}

.emailActive {
  font-size: 15px;
  color: #333333;
}
</style>
